﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ERP.RBAC.Dto;
using Microsoft.Extensions.Logging;
using Volo.Abp.Domain.Repositories;
namespace ERP.RBAC
{
    public class MenuServices : IMenuServices
    {
        private readonly IRepository<Menu> _menurepository;
        private readonly ILogger<MenuServices> _logger;

        public MenuServices(IRepository<Menu> repository, ILogger<MenuServices> logger)
        {
            _menurepository = repository;
            _logger = logger;
        }
        public async Task<List<MenuDto>> ShowMenu()
        {
            // 获取所有菜单
            var menuList = await _menurepository.GetListAsync();
            
            _logger.LogInformation($"Found {menuList.Count} menus in database");
            foreach (var menu in menuList)
            {
                _logger.LogInformation($"Menu: Id={menu.Id}, Name={menu.Name}, ParentId={menu.ParentId}");
            }
            
            // 将Menu转换为MenuDto
            var menuDtos = menuList.Select(m => new MenuDto
            {
                Id = m.Id,
                Name = m.Name,
                Url = m.Url,
                Icon = m.Icon,
                ParentId = m.ParentId,
                OrderNum = m.OrderNum,
                PermissionCode = m.PermissionCode,
                CreationTime = m.CreationTime
            }).OrderBy(m => m.OrderNum).ToList();
            
            return menuDtos;
        }
    }
}
